<template>
	<view class="container">
		<view class="content">
			<view class="topss">
				<view class="left">
					<uni-search-bar placeholder="搜索" @confirm="search" />
				</view>
				<view class="right">
					<uni-badge class="uni-badge-left-margin" :text="cartNum" absolute="rightTop" :offset="[5, 5]" size="small">
						<uni-icons type="cart" size="30" color="#000" @click="$public.navto('tjCart')"></uni-icons>
					</uni-badge>

				</view>
			</view>
			<view class="tabs">
				<view class="top_nav">
					<scroll-view id="tab-bar" class="scroll-h" :scroll-x="true" :show-scrollbar="true"
						:scroll-into-view="scrollInto">
						<view v-for="(tab,index) in tabBars" :key="tab.columnId" class="uni-tab-item" :id="tab.columnId"
							:data-current="index" @click="ontabtap" :class="tabIndex==index ? 'uni-tab-item-title-active' : ''">
							<text class="uni-tab-item-title">{{tab.title}}</text>
						</view>
					</scroll-view>
				</view>
				<view class="line-h"></view>
				<view class="newslist">
					<view class="list" v-for="item in newsList" :key="item.productId"
						@click="$public.navto('detail',{productId:item.productId,typeId:6})">
						<view class="left">
							<view class="imagearr">
								<image :src="$public.imgageUrl(item.smallImage)" mode="scaleToFill" class="image"></image>
							</view>
						</view>
						<view class="right">
							<view class="title">
								{{item.title}}
							</view>
							<view class="remark">
								{{item.remark}}
							</view>
							<view class="shengyu">
								<progress :percent="item.overBuy" stroke-width="10" activeColor="#DF686A" backgroundColor="#E6E6E6"
									border-radius="20" show-info />
							</view>
							<view class="jifen-div">
								<view class="jifen">
									<view class="xiangou" v-if="item.buyNum">
										每人限购{{item.buyNum}}份
									</view>
								</view>
								<!-- <view class="jf-number">
									<uni-number-box v-model="item.mynum" :max="item.buyNum?item.buyNum:500" @change="buyNumChange"
										class="numcss" background="#DF686A" color="#fff" />
								</view> -->
							</view>
							<view class="duihuantext">
								<view class="money">¥{{item.money}}</view>
								<view class="salse">原价:¥ {{item.market_price}}</view>
								<!-- <view class="yishou"> 已售 <text class="wenzi33">{{item.exchangeNum}}</text> </view> -->
								<view class="qiangbtn">
									<view class="btncss">
										立即抢购
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>

			<!-- <view class="page-bottom">
				<view class="page-bottom-l">
					<button open-type="contact" class="p-b-btn">
						<image src="../../static/sj_wljfimg2.png" mode="scaleToFill" class="navimage"></image>
						<text>客服</text>
					</button>
				</view>

				<view class="action-btn-group">
					<button type="primary" class=" btncss" @click="buttonClick()">购物车({{buyNum}})</button>
				</view>
			</view> -->
		</view>
	</view>

</template>

<script>
	export default {
		data() {
			return {
				notice: "",
				productList: [],
				changYongData: [],
				tzList: [],
				yyzdjList: [],
				cacheTab: [],
				tabIndex: 0,
				tabBars: [],
				scrollInto: "",
				showTips: false,
				showSS: false,
				newsList: [],
				allNewsList: [],
				buyNum: 0,
				cartNum: ""
			}
		},
		async onLoad() {
			this.getMallColumn();
			this.getProductList()
			// this.getArticle20();
		},
		onShow() {
			this.getCart()
		},
		methods: {
			getCart() {
				const value = uni.getStorageSync('tjData');
				let json = []
				if (value) {
					json = JSON.parse(value)
				}
				this.cartNum = json.length
			},
			change(e) {
				this.current = e.detail.current
			},
			search() {

			},
			ontabtap(e) {
				let index = e.target.dataset.current || e.currentTarget.dataset.current;
				// console.log("index", index)
				this.switchTab(index);
			},
			switchTab(index) {
				// if (this.tabIndex === index) {
				// 	return;
				// }
				this.newsList = []
				let word = this.word;
				if (index == 0) {
					if (word) {
						this.allNewsList.forEach(item => {
							if (item.title.indexOf(word) >= 0)
								this.newsList.push(item)
						})
					} else {
						this.allNewsList.forEach(item => {
							this.newsList.push(item)
						})
					}
				} else {
					let columnId = this.tabBars[index].columnId
					if (word) {
						this.allNewsList.forEach(item => {
							if (item.title.indexOf(word) >= 0 && item.columnId == columnId)
								this.newsList.push(item)
						})
					} else {
						this.allNewsList.forEach(item => {
							if (item.columnId == columnId) {
								this.newsList.push(item)
							}
						})
					}

				}
				this.tabIndex = index;
				this.scrollInto = this.tabBars[index].id;
			},
			getMallColumn() {
				this.$public.htmlPost("/api/getMallColumn", {
					parentId: 5
				}).then((res) => {
					console.log("getMallColumn res", res)
					let data = res.data;
					if (data.code == 0) {
						this.tabBars.push({
							columnId: 0,
							title: "全部"
						})

						data.data.forEach(item => {
							this.tabBars.push(item)
						})
					}
				})
			},
			getProductList() {
				let params = {
					orderByColumn: "createTime",
					isAsc: "desc",
					typeId: 5
				}
				if (this.word)
					params.keyword = this.word
				this.$public.htmlPost("/api/getProductList", params).then((res) => {
					console.log("getProductList res", res)
					let data = res.data;
					if (data.code == 0) {
						data.data.forEach(item => {
							let json = JSON.parse(item.referenceMoney)
							item.money = json.money
							item.market_price = json.market_price
							item.bigImageArr = item.bigImage.split(",")
							item.mynum = 0
							// console.log("item.exchangeNum ", item.exchangeNum)
							// console.log("json.inventory ", json.inventory)
							// console.log("json.exchangeNum+json.inventory", (parseInt(item.exchangeNum) + parseInt(json
							// 	.inventory)))
							// console.log(((parseInt(item.exchangeNum) / (parseInt(item.exchangeNum) + parseInt(json
							// 	.inventory))) * 100).toFixed(0))
							item.overBuy = ((parseInt(item.exchangeNum) / (parseInt(item.exchangeNum) + parseInt(json
								.inventory))) * 100).toFixed(0)
							this.newsList.push(item)
							// this.newsList.push(item)
							this.allNewsList.push(item)
						})
					}
					uni.hideLoading();
				})
			},
			async buttonClick() {
				let that = this
				let goodsData = []
				let flag = false
				this.newsList.forEach(item => {
					if (item.mynum > 0) {
						flag = true
						goodsData.push({
							productId: item.productId,
							num: item.mynum,
							model: "1-1",
							params: {
								product: {
									productId: item.productId,
									title: item.title
								}
							},
							title: item.title,
							image: item.smallImage,
							attr_val: "社区团购",
							price: 0,
							stock: 100,
							cartId: 0,
							money: item.money,
							typeId: 6,
							maxNum: item.buyNum,
							active: true
						})
					}
				})
				if (!flag) {
					this.$api.msg("最少选择一个商品")
					return false
				}
				this.$public.navto(`/pages/shangquan/cart`, {
					data: JSON.stringify({
						goodsData: goodsData
					})
				})
				// } else {
				// 	this.addBuyCart()
				// }
			},
			buyNumChange() {
				let num = 0
				this.newsList.forEach(item => {
					if (item.mynum > 0) {
						num++
					}
				})
				this.buyNum = num
			},

		},
	}
</script>

<style lang="scss" scoped>
	.content {
		// background-color: #fff;
		// background: #e37164;
		// background-size: 100% auto;

		.wrap {
			padding: $uni-wrap-padding;
		}

		.topss {
			display: flex;
			align-items: center;
			justify-content: space-between;

			.left {
				width: 650rpx;
			}

			.right {
				padding-right: 20rpx;
			}
		}

		.banner_div {
			padding: 30rpx 0;

			background: linear-gradient(180deg, #e37164 0%, #fff 50%);

			.banner_image {
				width: 100%;
				height: 368rpx;
			}

			.swiper-box {
				height: 344rpx;
			}

			.swiperimage {
				width: 100%;
				height: 100%;
				border-radius: 20rpx;
				// overflow: hidden;
			}
		}

		.tz-div {
			padding: 0 30rpx;
			display: flex;
			align-items: center;
			justify-content: space-around;
			background: url(#{$imageUrl}/tzbg.png) top center no-repeat;
			background-size: 100% auto;
			height: 180rpx;

			.tz-left {
				margin-right: 30rpx;

				.image {
					width: 74rpx;
					height: 72rpx;
				}
			}

			.tz-right {
				.tz-title {
					display: flex;
					justify-content: space-between;
					align-items: center;
					font-size: 32rpx;
					font-weight: bold;
					color: #333333;
					margin-bottom: 10rpx;

					.image {
						width: 32rpx;
						height: 32rpx;
					}
				}

				.tz-abstract {
					font-size: 24rpx;
					color: #666666;
				}
			}
		}

		.cygn-div {
			background: #fff;
			border-radius: 16rpx;
			padding: 30rpx 48rpx;
			margin-top: 30rpx;
		}

		.app-title {
			margin-bottom: 0;
		}

		.material-column {
			.image-div {
				// width: 116rpx;
				// height: 116rpx;
				// background: #f3f3f3;
				border-radius: 50%;
				display: flex;
				align-items: center;
				justify-content: space-around;
				margin: 40rpx auto 10rpx;

				.image {
					width: 136rpx;
					height: 136rpx;
				}
			}

			.text {
				text-align: center;
				width: 130rpx;
				color: #666666;
				margin: 0 auto;
				// line-height: 2;
				font-size: 28rpx;
			}
		}

	}

	.uucdj-div {
		margin-top: 40rpx;

		.uucdj {
			width: 100%;
			height: 188rpx;
		}
	}

	.jrjg-div {
		display: flex;
		justify-content: space-between;
		flex-wrap: wrap;

		.jrjg-list {
			width: 290rpx;
			background: #FAFAFA;
			border-radius: 16rpx;
			overflow: hidden;
			font-size: 28rpx;
			margin-bottom: 30rpx;

			.image {
				width: 290rpx;
				height: 290rpx;
			}

			.title1 {
				padding: 0 16rpx;
				color: #333333;
				margin-top: 24rpx;
			}

			.title2 {
				padding: 0 16rpx;
				margin: 8rpx 0 16rpx;
				color: #999999;
				font-size: 26rpx;

				.redzi {
					font-size: 32rpx;
				}
			}

			.title3 {
				padding: 0 16rpx;
				color: #666666;
				margin-bottom: 16rpx;
				display: flex;
				align-items: center;

				.image {
					width: 24rpx;
					height: 24rpx;
					margin-right: 15rpx;
				}
			}

			.redzi {
				color: #DF686A;
			}
		}
	}

	.tabs {
		flex: 1;
		flex-direction: column;
		overflow: hidden;
		background-color: #ffffff;
		padding: $uni-wrap-padding;
		padding-bottom: 180rpx;

		.ssimage {
			width: 37rpx;
			height: 40rpx;
		}

		.top_nav {
			display: flex;
			justify-content: space-between;
			align-items: center;
		}
	}

	.scroll-h {
		width: 630rpx;
		height: 80rpx;
		flex-direction: row;
		white-space: nowrap;
		/* flex-wrap: nowrap; */
		/* border-color: #cccccc;
		border-bottom-style: solid;
		border-bottom-width: 1px; */
	}

	.line-h {
		height: 1rpx;
		background-color: #cccccc;
	}

	.uni-tab-item {
		/* #ifndef APP-PLUS */
		display: inline-block;
		/* #endif */
		flex-wrap: nowrap;
		padding-left: 34rpx;
		padding-right: 34rpx;
		color: #555;
	}

	.uni-tab-item-title {
		font-size: 30rpx;
		height: 74rpx;
		line-height: 74rpx;
		flex-wrap: nowrap;
		white-space: nowrap;
	}

	.uni-tab-item-title-active {
		color: #DF686A;
		border-bottom: 6rpx solid #DF686A;
	}

	.newslist {
		// padding-top: 55rpx;
		// display: flex;
		// flex-wrap: wrap;
		// justify-content: space-between;
		// padding-bottom: 40rpx;

		.list {
			// width: 320rpx;
			// margin-bottom: 60rpx;
			padding: 40rpx 0;

			border-bottom: 1rpx solid rgba(0, 0, 0, 0.1);
			display: flex;
			align-items: center;
			justify-content: space-between;

			.left {
				width: 200rpx;
			}

			.right {
				width: 475rpx;
			}

			.imagearr {
				// display: flex;
				// justify-content: space-between;
				// padding: 24rpx 0;

				.image {
					width: 200rpx;
					height: 200rpx;
					border-radius: 10rpx;

				}
			}


			.title {
				font-size: 32rpx;
				color: #333333;
				// padding-top: 20rpx;
				line-height: 38rpx;
				// margin-bottom: 24rpx;
				white-space: nowrap;
				/* 阻止文本换行 */
				overflow: hidden;
				/* 隐藏溢出的文本 */
				text-overflow: ellipsis;
				/* 使用省略号表示被隐藏的文本 */
				max-width: 100%;
			}

			.remark {

				font-size: 28rpx;
				color: #999999;
				line-height: 33rpx;
				margin: 10rpx 0;
			}

			.jifen-div {
				display: flex;
				justify-content: space-between;

				.numcss {
					/deep/.uni-numbox-btns {
						border-radius: 50%;
					}

					/deep/.uni-numbox__value {
						background: #fff !important;
						color: #333 !important;
					}
				}
			}

			.jifen {
				display: flex;
				// justify-content: space-between;
				align-items: center;
				color: #ff9600;
				font-size: 32rpx;
				padding: 10rpx 0;

				.jifen-l {
					display: flex;
					align-items: center;
				}

				.jifenimage {
					width: 31rpx;
					height: 27rpx;
					margin-right: 10rpx;
				}

				.blue {
					color: #DF686A;
					font-size: 32rpx;
				}

				.xiangou {
					// background: #FF9466;
					border-radius: 25rpx;
					color: #DF686A;
					padding: 0 15rpx;
					font-size: 24rpx;
					border: 1px solid #DF686A;
					// margin-left: 16rpx;
					height: 40rpx;
					line-height: 40rpx;
				}
			}

			.duihuantext {
				color: #999999;
				font-size: 26rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;

				.salse {
					text-decoration: line-through;
					// text-align: right;
					margin-right: 40rpx;
					font-size: 24rpx;
					padding-left: 16rpx;
				}

				.money {
					color: #333333;
					font-size: 32rpx;
				}

				.yishou {
					.wenzi33 {
						color: #333333;
					}
				}

				.qiangbtn {
					width: 160rpx;
					height: 64rpx;

					.btncss {
						width: 160rpx;
						height: 64rpx;
						background: rgba(223, 104, 106, 0.10);
						font-size: 28rpx;
						color: #DF686A;
						letter-spacing: normal;
						font-weight: normal;
					}
				}
			}


		}
	}

	.page-bottom {
		position: fixed;
		left: 0;
		bottom: 0;
		z-index: 95;
		display: flex;
		justify-content: space-around;
		align-items: center;
		width: 100%;
		height: 140rpx;
		background: #f7f7f7;

		.navimage {
			width: 36rpx;
			height: 34rpx;
		}

		.page-bottom-l {
			display: flex;


		}

		// box-shadow: 0 0 20upx 0 rgba(0, 0, 0, .5);
		// border-radius: 16upx;
		uni-button {

			&.p-b-btn {
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
				font-size: $font-sm;
				color: $font-color-base;
				width: 96upx;
				height: 100rpx;
				margin: 0;
				padding: 0;
				line-height: 1.8;
				background: none;
			}

			&:after {
				display: none;
			}
		}

		.p-b-btn {
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			font-size: $font-sm;
			color: $font-color-base;
			width: 96upx;
			height: 100rpx;
			margin: 0;
			padding: 0;
			line-height: 1.6;
			background: none;

			&:after {
				display: none;
			}

			.yticon {
				font-size: 40upx;
				line-height: 48upx;
				color: $font-color-light;

				&.yituuu {
					margin-top: 10upx;
					line-height: 36upx;
				}
			}

			&.active,
			&.active .yticon {
				color: $uni-color-primary;
			}

			.icon-fenxiang2 {
				font-size: 42upx;
				transform: translateY(-2upx);
			}

			.icon-shoucang {
				font-size: 46upx;
			}
		}

		.action-btn-group {
			display: flex;
			width: 552rpx;
			height: 88rpx;
			border-radius: 10rpx;
			overflow: hidden;
			margin-left: 20upx;

			// position: relative;
			.btncss {
				height: 88rpx;

			}
		}
	}
</style>